package com.cuz.hot100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Q18FourNumSum {
    public static void main(String[] args) {


        class F {
            public List<List<Integer>> fourSum(int[] nums, int target) {
                if (nums == null || nums.length < 4) {
                    return Collections.emptyList();
                }

                Arrays.sort(nums);

                if (nums[0] + nums[1] + nums[2] + nums[3] > target) {
                    return Collections.emptyList();
                }

                if (nums[nums.length - 1] + nums[nums.length - 2] + nums[nums.length - 3] + nums[nums.length - 4] < target) {
                    return Collections.emptyList();
                }

                List<List<Integer>> result = new ArrayList<>();

                for (int i = 0; i < nums.length; i++) {
                    if (i > 0 && nums[i] == nums[i - 1]) {
                        continue;
                    }

                    for (int j = i + 1; j < nums.length; j++) {
                        if (j > i + 1 && nums[j] == nums[j - 1]) {
                            continue;
                        }
                        int left = j + 1;
                        int right = nums.length - 1;
                        while (left < right) {
                            if (nums[i] + nums[j] + nums[left] + nums[right] == target) {
                                result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                                while (left < right && nums[left] == nums[left + 1]) {
                                    left++;
                                }
                                left++;
                                while (left < right &&nums[right] == nums[right - 1]) {
                                    right--;
                                }
                                right--;
                            } else {
                                if (nums[i] + nums[j] + nums[left] + nums[right] > target) {
                                    right--;
                                }
                                if (nums[i] + nums[j] + nums[left] + nums[right] < target) {
                                    left++;
                                }

                            }
                        }

                    }
                }
                return result;
            }
        }

        List<List<Integer>> lists = new F()
                .fourSum(new int[]{0,0,0,1000000000,1000000000,1000000000,1000000000}, 1000000000);
        int[] arr = {1, 0, -1, 0, -2, 2};
        System.out.println(fourSum1(arr, 0));
        System.out.println(Solution.fourSum(arr, 0));
    }

    public static List<List<Integer>> fourSum1(int[] nums, int target) {
        if (nums == null || nums.length < 4) {
            return Collections.emptyList();
        }
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        //1 2 3 4  len=4  first<1
        int len = nums.length;
        for (int first = 0; first < len - 3; first++) {
            if (first > 0 && nums[first] == nums[first - 1]) {
                continue;
            }
            //前四个大于目标 不需要继续尝试了
            if (nums[first] + nums[first + 1] > target - nums[first + 2] - nums[first + 3]) {
                break;
            }
            //       第一个 加后三个 小于目标 说明first太小了
            if (nums[first] + nums[len - 3] < target - nums[len - 1] - nums[len - 2]) {
                continue;
            }
            for (int second = first + 1; second < len - 2; second++) {
                //第二个数去重复
                if (second > first + 1 && nums[second] == nums[second - 1]) {
                    continue;
                }
                //第1 第二 加后面两数一级大于了 不可能有新答案
                if (nums[first] + nums[second] > target - (nums[second + 1] + nums[second + 2])) {
                    break;
                }
                //最大还是小于 second太小了
                if (nums[first] + nums[second] < target - (nums[len - 1] + nums[len - 2])) {
                    continue;
                }
                int third = second + 1;
                int four = len - 1;
                while (third < four) {
                    long tempSum = (long) nums[first] + nums[second] + nums[third] + nums[four];
                    if (tempSum == target) {
                        res.add(Arrays.asList(nums[first], nums[second], nums[third], nums[four]));
                        //三四 去重
                        while (third < four && nums[third] == nums[third + 1]) {
                            third++;
                        }
                        third++;
                        while (third < four && nums[four] == nums[four - 1]) {
                            four--;
                        }
                        four--;
                    } else if (tempSum > target) {
                        third++;
                    } else {
                        four--;
                    }
                }
            }
        }
        return res;
    }

    static class Solution {
        public static List<List<Integer>> fourSum(int[] nums, int target) {
            if (nums == null || nums.length < 4) {
                return Collections.emptyList();
            }
            List<List<Integer>> resList = new ArrayList<>();
            Arrays.sort(nums);
            int length = nums.length;
            for (int first = 0; first < length - 3; first++) {
                if (first > 0 && nums[first] == nums[first - 1]) {
                    continue;
                }
                if ((long) nums[first] + nums[first + 1] + nums[first + 2] + nums[first + 3] > target) {
                    break;
                }
                if ((long) nums[first] + nums[length - 3] + nums[length - 2] + nums[length - 1] < target) {
                    continue;
                }
                for (int second = first + 1; second < length - 2; second++) {
                    if (second > first + 1 && nums[second] == nums[second - 1]) {
                        continue;
                    }
                    if ((long) nums[first] + nums[second] + nums[second + 1] + nums[second + 2] > target) {
                        break;
                    }
                    if ((long) nums[first] + nums[second] + nums[length - 2] + nums[length - 1] < target) {
                        continue;
                    }
                    int third = second + 1, four = length - 1;
                    while (third < four) {
                        int tempSum = nums[first] + nums[second] + nums[third] + nums[four];
                        if (tempSum == target) {
                            resList.add(Arrays.asList(nums[first], nums[second], nums[third], nums[four]));
                            while (third < four && nums[third] == nums[third + 1]) {
                                third++;
                            }
                            third++;
                            while (third < four && nums[four] == nums[four - 1]) {
                                four--;
                            }
                            four--;
                        } else if (tempSum < target) {
                            third++;
                        } else {
                            four--;
                        }
                    }
                }
            }
            return resList;
        }
    }
}
